﻿using System;
using System.Reflection;

namespace Flywheel.VSHelpers
{
	public class AppDomainPathResolver : IDisposable
	{
		private readonly AppDomain _domainToResolve;

		public AppDomainPathResolver(AppDomain domainToResolve)
		{
			_domainToResolve = domainToResolve;
			_domainToResolve.AssemblyResolve += ResolveAssembly;
		}

		public AppDomainPathResolver()
			: this(AppDomain.CurrentDomain)
		{
		}

		public string AssemblyPath { get; set; }



		public void Dispose()
		{
			_domainToResolve.AssemblyResolve -= ResolveAssembly;
		}


		private Assembly ResolveAssembly(object sender, ResolveEventArgs args)
		{
			return Assembly.LoadFrom(AssemblyPath);
		}
	}
}